package com.wenhui.schoolyardinfo.firstpage.adapter;

import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;

import com.wenhui.schoolyardinfo.firstpage.holder.SmartViewContentHolder;
import com.wenhui.schoolyardinfo.firstpage.holder.SmartViewFooterHolder;
import com.wenhui.schoolyardinfo.firstpage.holder.SmartViewHeaderHolder;
import com.wenhui.schoolyardinfo.firstpage.holder.SmartViewProfileFooterHolder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/15 14:14
 * introduce : 多功能adapter
 */

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter implements ListAdapter {

    private static final String TAG = "BaseRecyclerAdapter";
    private int mHeaderLayoutId;
    private int mContentLayoutId;
    private int mFooterLayoutId;
    private int mProfileFooterLayoutId;
    private final List<T> mList;
    private int mLastPosition = -1;
    private boolean mOpenAnimationEnable = true;
    private boolean isShowProfileFooter;
    private int layoutCount;
    private AdapterView.OnItemClickListener mListener;
    private final static int HEADER_COUNT = 1;
    private final static int CONTENT_COUNT = 1;
//    private final static int FOOTER_COUNT = 1;

    private final static int TYPE_HEADER = 1;
    private final static int TYPE_CONTENT = 2;
    private final static int TYPE_FOOTER = 3;
    private final static int TYPE_PROFILE_FOOTER = 4;

    public BaseRecyclerAdapter(@LayoutRes int contentLayoutId, int layoutCount) {
        setHasStableIds(false);
        this.mList = new ArrayList<>();
        this.mContentLayoutId = contentLayoutId;
        this.layoutCount = layoutCount;
    }

    public BaseRecyclerAdapter(
            @LayoutRes int headerLayoutId,
            @LayoutRes int contentLayoutId,
            @LayoutRes int footerLayoutId,
            int layoutCount) {
        setHasStableIds(false);
        this.mList = new ArrayList<>();
        this.mHeaderLayoutId = headerLayoutId;
        this.mContentLayoutId = contentLayoutId;
        this.mFooterLayoutId = footerLayoutId;
        this.layoutCount = layoutCount;
    }

    public BaseRecyclerAdapter(Collection<T> collection,
                               @LayoutRes int headerLayoutId,
                               @LayoutRes int contentLayoutId,
                               @LayoutRes int footerLayoutId,
                               @LayoutRes int profileFooterLayoutId,
                               int layoutCount) {
        setHasStableIds(false);
        this.mList = new ArrayList<>(collection);
        this.mHeaderLayoutId = headerLayoutId;
        this.mContentLayoutId = contentLayoutId;
        this.mFooterLayoutId = footerLayoutId;
        this.mProfileFooterLayoutId = profileFooterLayoutId;
        this.layoutCount = layoutCount;
    }

    public BaseRecyclerAdapter(Collection<T> collection, @LayoutRes int headerLayoutId, @LayoutRes int contentLayoutId, @LayoutRes int footerLayoutId, AdapterView.OnItemClickListener listener) {
        setHasStableIds(false);
        setOnItemClickListener(listener);
        this.mList = new ArrayList<>(collection);
        this.mHeaderLayoutId = headerLayoutId;
        this.mContentLayoutId = contentLayoutId;
        this.mFooterLayoutId = footerLayoutId;
    }

    public void clearData() {
        this.mList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<T> mList) {
        this.mList.addAll(mList);
        notifyDataSetChanged();
    }

    public void addAllData(List<T> mList, boolean isShowProfileFooter) {
        this.mList.addAll(mList);
        this.isShowProfileFooter = isShowProfileFooter;
        notifyDataSetChanged();
    }

    private void addAnimate(RecyclerView.ViewHolder holder, int postion) {
        if (mOpenAnimationEnable && mLastPosition < postion) {
            holder.itemView.setAlpha(0);
            holder.itemView.animate().alpha(1).start();
            mLastPosition = postion;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (layoutCount == 1) {
            return new SmartViewContentHolder(LayoutInflater.from(parent.getContext()).inflate(mContentLayoutId, parent, false), mListener);
        } else if (layoutCount == 2) {
            if (viewType == TYPE_HEADER) {
                return new SmartViewHeaderHolder(LayoutInflater.from(parent.getContext()).inflate(mHeaderLayoutId, parent, false), mListener);
            } else {
                return new SmartViewContentHolder(LayoutInflater.from(parent.getContext()).inflate(mContentLayoutId, parent, false), mListener);
            }
        } else if (layoutCount == 3) {
            if (viewType == TYPE_HEADER) {
                return new SmartViewHeaderHolder(LayoutInflater.from(parent.getContext()).inflate(mHeaderLayoutId, parent, false), mListener);
            } else if (viewType == TYPE_CONTENT) {
                return new SmartViewContentHolder(LayoutInflater.from(parent.getContext()).inflate(mContentLayoutId, parent, false), mListener);
            } else {
                return new SmartViewFooterHolder(LayoutInflater.from(parent.getContext()).inflate(mFooterLayoutId, parent, false), mListener);
            }
        } else {
            if (viewType == TYPE_HEADER) {
                return new SmartViewHeaderHolder(LayoutInflater.from(parent.getContext()).inflate(mHeaderLayoutId, parent, false), mListener);
            } else if (viewType == TYPE_CONTENT) {
                return new SmartViewContentHolder(LayoutInflater.from(parent.getContext()).inflate(mContentLayoutId, parent, false), mListener);
            } else if (viewType == TYPE_FOOTER) {
                return new SmartViewFooterHolder(LayoutInflater.from(parent.getContext()).inflate(mFooterLayoutId, parent, false), mListener);
            } else {
                return new SmartViewProfileFooterHolder(LayoutInflater.from(parent.getContext()).inflate(mProfileFooterLayoutId, parent, false), mListener);
            }
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindViewHolder(holder, position < mList.size() ? mList.get(position) : null, position);
    }

    protected abstract void onBindViewHolder(RecyclerView.ViewHolder holder, T model, int position);

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        addAnimate(holder, holder.getLayoutPosition());
    }

    public void setOpenAnimationEnable(boolean enabled) {
        this.mOpenAnimationEnable = enabled;
    }

    public BaseRecyclerAdapter<T> setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        mListener = listener;
        return this;
    }

    public BaseRecyclerAdapter<T> refresh(Collection<T> collection) {
        mList.clear();
        mList.addAll(collection);
        notifyDataSetChanged();
        notifyListDataSetChanged();
        mLastPosition = -1;
        return this;
    }

    public BaseRecyclerAdapter<T> loadMore(Collection<T> collection) {
        mList.addAll(collection);
        notifyDataSetChanged();
        notifyListDataSetChanged();
        return this;
    }

    private final DataSetObservable mDataSetObservable = new DataSetObservable();

    public void registerDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.registerObserver(observer);
    }

    public void unregisterDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.unregisterObserver(observer);
    }

    /**
     * Notifies the attached observers that the underlying data has been changed
     * and any View reflecting the data set should refresh itself.
     */
    public void notifyListDataSetChanged() {
        mDataSetObservable.notifyChanged();
    }

    /**
     * Notifies the attached observers that the underlying data is no longer valid
     * or available. Once invoked this adapter is no longer valid and should
     * not report further data set changes.
     */
    public void notifyDataSetInvalidated() {
        mDataSetObservable.notifyInvalidated();
    }

    public boolean areAllItemsEnabled() {
        return true;
    }

    public boolean isEnabled(int position) {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        RecyclerView.ViewHolder holder = null;
        if (layoutCount == 1) {
            SmartViewContentHolder smartViewContentHolder = (SmartViewContentHolder) holder;
            if (convertView != null) {
                smartViewContentHolder = (SmartViewContentHolder) convertView.getTag();
            } else {
                smartViewContentHolder = (SmartViewContentHolder) onCreateViewHolder(parent, getItemViewType(position));
                convertView = smartViewContentHolder.itemView;
                convertView.setTag(holder);
            }
            smartViewContentHolder.setPosition(position);
            onBindViewHolder(holder, position);
            addAnimate(holder, position);
        } else {
            if (holder instanceof SmartViewHeaderHolder) {
                SmartViewHeaderHolder smartViewHeaderHolder = (SmartViewHeaderHolder) holder;
                if (convertView != null) {
                    smartViewHeaderHolder = (SmartViewHeaderHolder) convertView.getTag();
                } else {
                    smartViewHeaderHolder = (SmartViewHeaderHolder) onCreateViewHolder(parent, getItemViewType(position));
                    convertView = smartViewHeaderHolder.itemView;
                    convertView.setTag(holder);
                }
                smartViewHeaderHolder.setPosition(position);
                onBindViewHolder(holder, position);
                addAnimate(holder, position);
            } else if (holder instanceof SmartViewContentHolder) {
                SmartViewContentHolder smartViewContentHolder = (SmartViewContentHolder) holder;
                if (convertView != null) {
                    smartViewContentHolder = (SmartViewContentHolder) convertView.getTag();
                } else {
                    smartViewContentHolder = (SmartViewContentHolder) onCreateViewHolder(parent, getItemViewType(position));
                    convertView = smartViewContentHolder.itemView;
                    convertView.setTag(holder);
                }
                smartViewContentHolder.setPosition(position);
                onBindViewHolder(holder, position);
                addAnimate(holder, position);
            } else if (holder instanceof SmartViewFooterHolder) {
                SmartViewFooterHolder smartViewFooterHolder = (SmartViewFooterHolder) holder;
                if (convertView != null) {
                    smartViewFooterHolder = (SmartViewFooterHolder) convertView.getTag();
                } else {
                    smartViewFooterHolder = (SmartViewFooterHolder) onCreateViewHolder(parent, getItemViewType(position));
                    convertView = smartViewFooterHolder.itemView;
                    convertView.setTag(holder);
                }
                smartViewFooterHolder.setPosition(position);
                onBindViewHolder(holder, position);
                addAnimate(holder, position);
            } else {
                SmartViewProfileFooterHolder smartViewProfileFooterHolder = (SmartViewProfileFooterHolder) holder;
                if (convertView != null) {
                    smartViewProfileFooterHolder = (SmartViewProfileFooterHolder) convertView.getTag();
                } else {
                    smartViewProfileFooterHolder = (SmartViewProfileFooterHolder) onCreateViewHolder(parent, getItemViewType(position));
                    convertView = smartViewProfileFooterHolder.itemView;
                    convertView.setTag(holder);
                }
                smartViewProfileFooterHolder.setPosition(position);
                onBindViewHolder(holder, position);
                addAnimate(holder, position);
            }
        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        if (layoutCount == 1) {//只有身体
            return TYPE_CONTENT;
        }else {
            if (!isShowProfileFooter) {
                if (position == 0) {//头部
                    return TYPE_HEADER;
                } else if (position == 7) {//尾部
                    return TYPE_FOOTER;
                } else {//身体
                    return TYPE_CONTENT;
                }
            } else {
                if (position == 0) {//头部
                    return TYPE_HEADER;
                } else if (position == 7) {//尾部简介
                    return TYPE_PROFILE_FOOTER;
                } else if (position == 8) {//尾部
                    return TYPE_FOOTER;
                } else {//身体
                    return TYPE_CONTENT;
                }
            }
        }
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean isEmpty() {
        return getCount() == 0;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

}
